//
//  DDShareAccountViewController.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/10/17.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDShareAccountViewController.h"
#import "DDShareAccountTableViewCell.h"
#import "DDAddShareAccountViewController.h"

#import "DDMoneybagHomepageService.h"

#import "DDShareAccountListModel.h"

@interface DDShareAccountViewController ()

@property(nonatomic ,strong)NSMutableArray *sectionArray;

@property(nonatomic,strong)DDMoneybagHomepageService *moneybagHomepageService;

@property(nonatomic ,strong)NSString *deleteResult;

@property(nonatomic ,assign)NSInteger recodeDeleteSection;

@end

@implementation DDShareAccountViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
    
    [self setupData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"共享账户";
    
    self.sectionArray = [NSMutableArray array];
    
    self.moneybagHomepageService = [[DDMoneybagHomepageService alloc]init];
    
    [self addNavigationRightBar];
    
    [self configureTableView];
}

- (void)addNavigationRightBar
{
    UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 66, 44)];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame= CGRectMake(32, 1, 42, 42);
    [rightButton setImage:[UIImage imageNamed:@"add+"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    [rightBarView addSubview:rightButton];
    rightBarView.backgroundColor=[UIColor clearColor];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

- (void)addAction
{
    DDAddShareAccountViewController *addShareAccountVC = [[DDAddShareAccountViewController alloc]init];
    addShareAccountVC.title = @"新增共享账户";
    [self.navigationController pushViewController:addShareAccountVC animated:YES];
}

- (void)configureTableView
{
    self.tableView.rowHeight = 50;
    WEAKSELF
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.view);
        make.left.mas_equalTo(weakSelf.view);
        make.right.mas_equalTo(weakSelf.view);
        make.bottom.mas_equalTo(weakSelf.view);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark  UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DDShareAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shareAccount"];
    if (cell == nil) {
        
        cell = [[DDShareAccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"shareAccount"];
    }
    
    cell.dataModel = [DDShareAccountListModel yy_modelWithJSON:self.sectionArray[indexPath.section]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return TableViewSectionHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == self.sectionArray.count-1) {
      
        return 200;
    }else{
    
        return 1;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == self.sectionArray.count-1){
    
    UIView *sectionFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,200)];
    
    UILabel *remarks = [[UILabel alloc]init];
    remarks.numberOfLines = 0;
    remarks.textColor = COLOR_FONT_LIGHT_GRAY;
    NSMutableAttributedString *attr = [DDUtils attrWithString:10 font:[UIFont systemFontOfSize:14] text:@"注：\n   ①共享账户即与该账户共享余额，对方可使用你的资金渠道进行支付，支持购物交易。\n   ②向左滑动删除信息。"];
    remarks.attributedText = attr;
    [sectionFooterView addSubview:remarks];
        
    [remarks mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(sectionFooterView).offset(20);
        make.left.mas_equalTo(sectionFooterView).offset(15);
        make.right.mas_equalTo(sectionFooterView).offset(-15);
    }];
    return sectionFooterView;
        
    }else{
    
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DDAddShareAccountViewController *addShareAccountVC = [[DDAddShareAccountViewController alloc]init];
    addShareAccountVC.title = @"修改共享账户";
    addShareAccountVC.adminAccountMessage = @[[NSString stringWithFormat:@"%@",self.sectionArray[indexPath.section][@"remark"]],[NSString stringWithFormat:@"%@",self.sectionArray[indexPath.section][@"mobile"]]];
    addShareAccountVC.currentID = [NSString stringWithFormat:@"%@",self.sectionArray[indexPath.section][@"id"]];
    [self.navigationController pushViewController:addShareAccountVC animated:YES];
}

#pragma mark  UITableViewDelegate  Delete相关
//定义编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleDelete;//默认没有
    return result;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
        self.recodeDeleteSection = indexPath.section;
        [self deleteShareAccountWithCurrentID:[NSString stringWithFormat:@"%@",self.sectionArray[indexPath.section][@"id"]]];
    }
}


// 网络请求
- (void)setupData
{
    
    WEAKSELF
    [self.moneybagHomepageService queryShareAccountSuccessBlock:^(id responseObject) {
        
        self.sectionArray = responseObject[@"data"][@"list"];

        [self performSelectorOnMainThread:@selector(backToMainToUpdata) withObject:self waitUntilDone:YES];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf requestFaild];
    }];
}

- (void)backToMainToUpdata
{
    
    [self.tableView reloadData];
}


// 删除共享账户
- (void)deleteShareAccountWithCurrentID:(NSString *)currentID
{
    
    WEAKSELF
    
    [self.moneybagHomepageService deleteAccountWithid:currentID SuccessBlock:^(id responseObject) {

        self.deleteResult = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
        
        [self performSelectorOnMainThread:@selector(backToMainToUpdata2) withObject:self waitUntilDone:YES];
        
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

- (void)backToMainToUpdata2
{
    WEAKSELF
    if ([self.deleteResult isEqualToString:@"成功！"]) {
        
        [self setupData];
    }else{
        
        [weakSelf showLDAlertView:@"删除失败"];
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [self.rdv_tabBarController setTabBarHidden:false animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
